//
//  MessageContent.m
//  极致荟
//
//  Created by hetao on 14-8-29.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "MessageContent.h"
#import "Note.h"
#import "NoteDao.h"
#import "OtherUsers.h"
#import "OtherUsersDao.h"
#import <AudioToolbox/AudioToolbox.h>
#import "FriendRequest.h"
@implementation MessageContent

+ (void)coedMessage:(id)message
{
    NSData *data= [message dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *messagedic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"收到的消息为：%@", messagedic );
    if([[messagedic objectForKey:@"type"] isEqualToString:@"sendMessage"])//收到的好友消息
    {
        [self sendMessage:messagedic];
    }

}

//收到好友消息操作
+ (void)sendMessage:(NSDictionary *)messagedic
{
    OtherUsers *ouser=[[OtherUsers alloc] init];
    
    Note *note=[[Note alloc]init];
    NSDictionary *content=[[messagedic objectForKey:@"data"] objectForKey:@"content"];
    note.content=[ content objectForKey:@"text"];
    note.noteType=[[content objectForKey:@"messageType"] intValue];
    note.vodeleng=[content objectForKey:@"vodeleng"];
    note.senderId=[[[messagedic objectForKey:@"data"] objectForKey:@"pushUserId"] intValue];
    note.recipientId=[User defuceUser].userId;
    note.noteDate=[NSDate date];
    [NoteDao insertData:note];//添加到数据库
    
    ouser.userId=note.senderId;
    ouser.note=note;
    
    [OtherUsersDao NoteMessage:ouser];//添加最近聊天记录
    [OtherUsersDao upNuber:ouser];//修改未查看聊天记录数
    
    [[NSNotificationCenter defaultCenter] postNotificationName:SENDMESSAGE object:self];
    SystemSoundID sound =  kSystemSoundID_Vibrate;
    AudioServicesPlaySystemSound(sound);
}

+ (void)sendFinderRequest
{
    [FriendRequest HttpFriendRequestcomYes:^(NSMutableArray *arr, NSString* rptime) {
    
        [[NSNotificationCenter defaultCenter] postNotificationName:SENDREQUEST object:self];
    }];
}
@end
